from django.core.management.base import BaseCommand
from django.utils import timezone
from users.models import UserProfile


class Command(BaseCommand):
    help = '修复 UserProfile 中 created_at 为 NULL 的历史数据'

    def handle(self, *args, **options):
        fixed = 0
        now = timezone.now()
        for up in UserProfile.objects.filter(created_at__isnull=True):
            up.created_at = now
            up.save(update_fields=['created_at'])
            fixed += 1
        self.stdout.write(self.style.SUCCESS(f'Fixed {fixed} UserProfile records'))